package com.situ.mallsdauweb.service.impl;

import com.situ.mallsdauweb.entity.Cart;
import com.situ.mallsdauweb.entity.Member;
import com.situ.mallsdauweb.exception.NotLoginException;
import com.situ.mallsdauweb.mapper.CartMapper;
import com.situ.mallsdauweb.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mallsdauweb.util.RequestUtil;
import com.situ.mallsdauweb.vo.CartVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author han
 * @since 2023-06-04
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Override
    public void addCart(Integer id) {
        //Integer memberId=1;//用户id
        Member m= RequestUtil.getCurrent();
        if(m==null){
            throw new NotLoginException();
        }
        Integer memberId=m.getId();
        Cart old =baseMapper.selectByMemberIdAndProductId(memberId,id);

        if(old!=null) {//之前加过
            old.setAmount(old.getAmount()+1);
            baseMapper.updateById(old);
        }else {

            Cart cart = new Cart();
            cart.setProductId(id);
            cart.setCreateTime(LocalDateTime.now());
            cart.setAmount(1);
            cart.setMemberId(memberId);//获取当前登录会员的id，
            baseMapper.insert(cart);
        }
    }

    @Override
    public List<CartVO> cartList() {
        Member m=RequestUtil.getCurrent();

        return baseMapper.cartList(m.getId());


    }

    @Override
    public List<CartVO> confirmCartList(String cartIds) {
        return baseMapper.selectCarts(cartIds.split(","));
    }
}
